package adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import model.PictureInfo;
import model.PostInfo;
import activity.PictureDetailActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fedorvlasov.lazylist.ImageSizeLoader;

import csf.getmoney.R;

public class FAQAdapter extends BaseExpandableListAdapter {
	Context context;
	ArrayList<PostInfo> faqList = new ArrayList<PostInfo>();
	LayoutInflater sa;

	ImageSizeLoader imageSizeLoader;

	Map<String, Boolean> map = new HashMap<String, Boolean>();

	public FAQAdapter(Context context, ArrayList<PostInfo> faqList) {
		this.context = context;
		this.sa = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.faqList = faqList;

		imageSizeLoader = new ImageSizeLoader(context, 1);
	}

	@Override
	public PostInfo getChild(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return faqList.get(arg0);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		convertView = sa.inflate(csf.getmoney.R.layout.adapter_faq_group, null);
		ImageView indicatorImage = (ImageView) convertView.findViewById(R.id.indicatorImage);
		TextView titleText = (TextView) convertView.findViewById(R.id.titleText);

		titleText.setText(faqList.get(groupPosition).getTitle());

		if (isExpanded) {
			indicatorImage.setImageResource(R.drawable.btn_down);
		} else {
			indicatorImage.setImageResource(R.drawable.btn_forward);
		}

		return convertView;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		convertView = sa.inflate(R.layout.adapter_faq_child, null);

		final LinearLayout row = (LinearLayout) convertView.findViewById(R.id.row);
		ImageView imageView = (ImageView) convertView.findViewById(R.id.photoImage);
		TextView bodyText = (TextView) convertView.findViewById(R.id.bodyText);

		imageView.setVisibility(View.VISIBLE);
		bodyText.setVisibility(View.GONE);

		final String url = faqList.get(groupPosition).getUrlImage().toString();
		if (!url.equals(""))
			imageSizeLoader.DisplayImage(url, imageView);

		imageView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, PictureDetailActivity.class);
				ArrayList<PictureInfo> picList = new ArrayList<PictureInfo>();
				PictureInfo picInfo = new PictureInfo();
				picInfo.setSourceUrl(url);
				picList.add(picInfo);

//				PictureDetailActivity.photos = picList;
				intent.putExtra("position", childPosition);
				((Activity) context).startActivity(intent);

			}
		});

		// if (eventList.get(groupPosition).getUrlImage().size() - 1 ==
		// childPosition) {
		bodyText.setVisibility(View.VISIBLE);
		bodyText.setText(faqList.get(groupPosition).getBody());
		// } else {
		// bodyText.setVisibility(View.GONE);
		// }

		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public PostInfo getGroup(int groupPosition) {
		return faqList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return faqList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return false;
	}

}
